
package uk.org.cardboardbox.nfcpointstracker.dialog;

import uk.org.cardboardbox.nfcpointstracker.R;
import uk.org.cardboardbox.nfcpointstracker.model.Tag;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

public class UserDetailDialog extends AlertDialog {

    private final Tag tag;

    private final Listener listener;

    private ImageView photo;

    public interface Listener {
        public void onSelectPhoto();
    }

    public UserDetailDialog(Context context, Tag tag, Listener listener) {
        super(context);
        init(context);
        this.tag = tag;
        this.listener = listener;
    }

    private final void init(Context context) {
        setTitle("Tag user details");
        LayoutInflater li = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View content = li.inflate(R.layout.userdetail, null);
        photo = (ImageView)content.findViewById(R.id.userdetail_photo);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelectPhoto();
            }
        });

        setView(content);
        setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        setButton(BUTTON_POSITIVE, context.getText(android.R.string.ok), new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
    }

    public final void onTagChanged() {
        byte[] photobytes = tag.getPhoto();
        Bitmap bm = BitmapFactory.decodeByteArray(photobytes, 0, photobytes.length);
        photo.setImageBitmap(bm);
    }
}
